package com.colin.util.design.chain;

import com.alibaba.fastjson.JSONObject;
import com.colin.util.design.chain.base.ChainDto;
import com.colin.util.design.chain.base.ChainResponse;
import com.colin.util.design.chain.base.ChainTemplate;
import com.colin.util.design.chain.request.PlatWithdrawDealRequest;
import com.colin.util.enums.ResponseStatusEnum;
import com.colin.util.exception.BusiException;
import com.colin.util.util.TransIdGenerator;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

@Component
public class PlatWithdrawDealStarter {
    
    private static final Logger logger = LoggerFactory.getLogger(ChainTemplate.class);

    @Resource
    private ChainContext chainContext;
    
    public ChainResponse startChain(PlatWithdrawDealRequest request) {
        ChainDto chainDto = new ChainDto();
        chainDto.setRequest(request);
        ChainResponse response = new ChainResponse();
        response.setStatus(ResponseStatusEnum._200.getStatus());
        response.setMessage(ResponseStatusEnum._200.getDesc());
        chainDto.setResponse(response);
        String s = TransIdGenerator.randomUUID();
        MDC.put("traceId", s);
        try{

            chainContext.nextPlatWithdrawDeal(chainDto);

        }catch (BusiException be){
            response.setStatus(be.getCode());
            response.setMessage(be.getMessage());
            logger.error("PlatWithdrawDealChain_busi:" + be.getMessage(), be);
        } catch (Exception e){
            response.setStatus(ResponseStatusEnum._500.getStatus());
            response.setMessage(ResponseStatusEnum._500.getDesc());
            logger.error("PlatWithdrawDealChain_error:", e);
        } finally {
            logger.info("PlatWithdrawDealStarter startChain trace:{}", JSONObject.toJSONString(chainDto));
            MDC.clear();
        }
        return response;
    }

    public static void main(String[] args) {
        System.out.println(5%4);
    }
}
